package com.raidpoints.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Button;

public class NoteBox extends DialogBox {
	RaidPoints rp;
	
	public NoteBox(RaidPoints mrp) {
		rp = mrp;
		setHTML("Notification Settings");
		
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("269px", "148px");
		
		Label lblEmail = new Label("Email:");
		layoutPanel.add(lblEmail);
		layoutPanel.setWidgetLeftWidth(lblEmail, 23.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblEmail, 30.0, Unit.PX, 16.0, Unit.PX);
		
		final TextBox emailBox = new TextBox();
		layoutPanel.add(emailBox);
		layoutPanel.setWidgetLeftWidth(emailBox, 75.0, Unit.PX, 180.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(emailBox, 17.0, Unit.PX, 32.0, Unit.PX);
		
		final IntegerBox hourBox = new IntegerBox();
		hourBox.setVisibleLength(3);
		hourBox.setValue(0);
		layoutPanel.add(hourBox);
		layoutPanel.setWidgetLeftWidth(hourBox, 209.0, Unit.PX, 44.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(hourBox, 68.0, Unit.PX, 26.0, Unit.PX);
		
		Label lblNewLabel = new Label("Email if slotted X hours before raid (0 for no notification):");
		layoutPanel.add(lblNewLabel);
		layoutPanel.setWidgetLeftWidth(lblNewLabel, 23.0, Unit.PX, 180.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel, 55.0, Unit.PX, 39.0, Unit.PX);
		
		Button saveButton = new Button("Save");
		layoutPanel.add(saveButton);
		layoutPanel.setWidgetLeftWidth(saveButton, 33.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(saveButton, 100.0, Unit.PX, 28.0, Unit.PX);
		
		Button closeButton = new Button("Close");
		layoutPanel.add(closeButton);
		layoutPanel.setWidgetLeftWidth(closeButton, 141.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(closeButton, 100.0, Unit.PX, 28.0, Unit.PX);
		
		AsyncCallback<String> infoCallback = new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String result) {
				String[] split = result.split(",");
				if(split.length != 2) return;
				emailBox.setText(split[0]);
				hourBox.setValue(Integer.parseInt(split[1]));
			}
			
		};
		rp.rpsvc.GetNotInfo(infoCallback);
		
		closeButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});
		
		
		saveButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				rp.rpsvc.SetNotInfo(emailBox.getText() + "," + Integer.toString(hourBox.getValue()), rp.stringCallback);
				hide();				
			}
		});
	}
}
